﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using FbKPI.Model;
using FbKPI.Facade;
using FbKPI.Facade.Validator;
using System.Web.Script.Serialization;
using System.Collections.ObjectModel;
using System.Web.Script.Services;
using System.Transactions;

namespace FBKPI.ProcessRequest
{
    /// <summary>
    /// Summary description for PrivilegeHandler
    /// </summary>
    public class PrivilegeHandler : IHttpHandler
    {

        public void ProcessRequest(HttpContext context)
        {
            context.Response.ContentType = "text/html";
            string msg = this.Save();
            context.Response.Write(msg);
        }

        public bool IsReusable
        {
            get
            {
                return false;
            }
        }

        private string Save()
        {
            string msg = "OK";
            string action = HttpContext.Current.Request.QueryString["action"];
            int ID = Convert.ToInt16(HttpContext.Current.Request.QueryString["ID"]);
            string Description = HttpContext.Current.Request.QueryString["Description"];
            bool isAsignee = Convert.ToBoolean(HttpContext.Current.Request.QueryString["isAssignee"]);
            bool isBreakDown = Convert.ToBoolean(HttpContext.Current.Request.QueryString["isBreakDown"]);
            bool isActive = Convert.ToBoolean(HttpContext.Current.Request.QueryString["isActive"]);
            bool isPickKPI = Convert.ToBoolean(HttpContext.Current.Request.QueryString["isPickKPI"]);
            bool isAddKPI = Convert.ToBoolean(HttpContext.Current.Request.QueryString["isAddKPI"]);
            Employee CurrUser = new EmployeeFacade().GetEmployeeByID(Convert.ToInt32(HttpContext.Current.Request.QueryString["CurrUser"]));

            PrivilegeFacade df = new PrivilegeFacade();
            Privilege Privileges = df.GetPrivilegeByID(ID);
            PrivilegeValidator dv = new PrivilegeValidator();

            if (Privileges == null)
            {
                Privileges = new Privilege();
                Privileges.ID = ID;
                Privileges.Description = Description;
                Privileges.isAssignee = isAsignee;
                Privileges.isBreakDown = isBreakDown;
                Privileges.isActive = isActive;
                Privileges.CreateBy = CurrUser.Email;
                Privileges.isAddKPI = isAddKPI;
                Privileges.isPickKPI = isPickKPI;
                msg = dv.ValidateOnSave(Privileges);
            }
            else if (action != "delete")
            {
                Privileges.Description = Description;
                Privileges.isAssignee = isAsignee;
                Privileges.isBreakDown = isBreakDown;
                Privileges.isActive = isActive;
                Privileges.LastModifiedBy = CurrUser.Email;
                Privileges.isAddKPI = isAddKPI;
                Privileges.isPickKPI = isPickKPI;
                msg = dv.ValidateOnUpdate(Privileges);
            }
            else if(action == "delete")
                Privileges.LastModifiedBy = CurrUser.Email;

            if (msg == "OK")
            {
                using (TransactionScope trans = new TransactionScope(TransactionScopeOption.RequiresNew))
                {
                    switch (action)
                    {
                        case "save":
                            msg = df.Save(Privileges);
                            break;
                        case "delete":
                            msg = df.Delete(Privileges);
                            break;
                    }
                    trans.Complete();
                    trans.Dispose();
                }
            }

            return msg;
        }
    }
}